import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:msh_checkbox/msh_checkbox.dart';
import 'package:smartloan_mdc/main.dart';
import 'package:smartloan_mdc/utils/locator/app_config.dart';
import 'package:smartloan_mdc/utils/locator/locator.dart';
import '../../res/string.dart';
import '../../res/theme.dart';
import '../../route/app_routes.dart';

class Term extends StatefulWidget {
  Term({
    Key? key,
    this.isAgree = false,
    this.checkBoxOnPress
  }) : super(key: key);

  late bool isAgree;
  final Function? checkBoxOnPress;

  @override
  State<Term> createState() => _TermState();
}

class _TermState extends State<Term> {

  // bool isAgree = true;

  @override
  Widget build(BuildContext context) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        MSHCheckbox(
          value: widget.isAgree,
          duration: Duration(milliseconds: 200),
          colorConfig: MSHColorConfig.fromCheckedUncheckedDisabled(
            checkedColor: Colors.white,
            uncheckedColor: Colors.grey
          ),
          onChanged: (value) {
            setState(() {
              widget.isAgree = value;
            });
            widget.checkBoxOnPress?.call(widget.isAgree);
          }
        ),
        SizedBox(width: 10.w),
        Expanded(
            child: Text.rich(
              textSpan(
                  AppStr.prefix,
                  false,
                  children: [
                    textSpan(AppStr.termsOfService, true, onTap: (){
                      // debugPrint('用户协议');
                      Get.toNamed(AppRoutes.h5, arguments: {
                        "url": "${locator<AppConfig>().getAppConfig().baseUrl}/#/termsCondition"});
                    }),
                    textSpan(AppStr.and, false),
                    textSpan(AppStr.privacyPolicy, true, onTap: (){
                      // debugPrint('隐私协议');
                      Get.toNamed(AppRoutes.h5, arguments: {
                        "url": "${locator<AppConfig>().getAppConfig().baseUrl}/#/provicy"});
                    }),
                    textSpan(AppStr.end, false),
                  ]
              ),
              softWrap: true,
            )
        )
      ],
    );
  }

  TextSpan textSpan(String text, bool highLight, {Function? onTap, List<InlineSpan>? children}) {
    TextStyle normalStyle = const TextStyle(fontSize: 12, color: Colors.white);
    TextStyle highLightStyle = TextStyle(fontSize: 12, color: Colors.white, fontWeight: FontWeight.bold);

    return TextSpan(
        text: text,
        style: highLight == true ? highLightStyle : normalStyle,
        children: children,
        recognizer: TapGestureRecognizer()
          ..onTap = () {
            onTap?.call();
          }
    );
  }
}